<?php

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
/**
 * Description of QuestionController
 *
 * @author hoangle
 */
class ClientController extends Zend_Controller_Action {

    private $sessionLogin;
    
    public function init() {
//        //Check session
//        $this->sessionLogin = new Zend_Session_Namespace('User');
//        //Check session
//        if (empty($this->sessionLogin->accountinfo)) {
//            $this->_redirect('/');
//            exit;
//        }
    }
    
    public function indexAction() {
        $this->view->headTitle('Question | Front End');
    }

    /**
     * @desc insert question of user
     */
    public function setAction() {
        //Check data submit
        if ($this->_request->isPost()) {
            $arrData = $this->_request->getPost();

            $arrData['mack'] = array_unique($arrData['mack']);

            $userid = $this->sessionLogin->accountinfo['userid'];
            $arrData['userid'] = $userid;
            //Validate data input
            $this->_validateRegister($arrData, $errorBool);

            //Insert data to DB
            if (!$errorBool) {
                foreach ($arrData['mack'] as $k => $qid) {
                    if (!empty($qid)) {
                        $arrInput = NULL;
                        $arrInput = array(
                            'userid' => $userid,
                            'questionid' => $qid,
                            'amount' => $arrData['amount'][$k],
                            'total' => $arrData['tongck'][$k]
                        );
                        $result = Service_Model::factory('Question', 'insertUserQuestion', $arrInput, 'frontend');
                    }
                }
                //End for
                if($result) {
                    $this->_redirect('question/success');
                }
            }
            //End if
        }
        //End post

        $arrQuestion = (array) Service_Model::factory('Question', 'getQuestion', NULL, 'frontend');
        $this->view->question = $arrQuestion;
    }
    //End set func
    
    /**
     * @desc Report set question success
     */
    public function successAction() {
        $this->view->flag = 'Bạn đã đặt câu hỏi thành công, vui lòng theo dõi thông tin kết quả tư vấn từ hệ thống.';
    }

    public function listAction() {
        $userid = $this->sessionLogin->accountinfo['userid'];
        $arrQuestion = (array) Service_Model::factory('Question', 'getQuestion', NULL, 'frontend');
        foreach($arrQuestion as $val){
            $arrRestQuest[$val['mackid']] = $val['mackname'];
        }


        $arrUserQuestion = (array) Service_Model::factory('Question', 'getUserQuestionByUserId', $userid, 'frontend');
        $this->view->question = $arrRestQuest;
        $this->view->userquestion = $arrUserQuestion;
    }


    /**
     * @desc get list question recent many user set
     * @return array data
     */
    public function recentAction() {
        $result = Service_Model::factory('Question', 'getManyQuestionRequireRecent', NULL, 'frontend');
        echo '<pre>';
        print_r($result);
        die;
    }


    
    ////////////////////////////////////////////////////////////////////////////
    //                          PRIVATE FUNC                                  //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @desc Check validate data input
     * @param <type> $arrData
     * @param <type> $errorBool
     */
    private function _validateRegister(& $arrData, & $errorBool) {
        $errorBool = false;
        $arrResult = array();
        $i = 0;

        foreach ($arrData['mack'] as $data) {
            if (!empty($data)) {
                $arrResultMa[] = $data;
                $arrResultAmount[] = $arrData['amount'][$i];
                $arrResultTotal[] = $arrData['tongck'][$i];
            }
            $i++;
        }
        $arrData['mack'] = $arrResultMa;
        $arrData['amount'] = $arrResultAmount;
        $arrData['tongck'] = $arrResultTotal;

        //Check validate ma chung khoan
        if (!empty($arrData['mack'])) {
            $arrBoolMaCK = array_map('is_numeric', $arrData['mack']);
            if (in_array(false, $arrBoolMaCK)) {
                $errorBool = true;
                $this->view->errorMaCK = 'Mã chứng khoán không hợp lệ';
            }
        }
        else {
            $errorBool = true;
            $this->view->errorMaCK = 'Hãy nhập mã chứng khoán';
        }
        //End mack

        //Check validate so luong
        if (!empty($arrData['amount'])) {
            $arrBoolMaCK = false;
            foreach ($arrData['amount'] as $mount) {
                if ($mount < 0) {
                    $arrBoolMaCK = true;
                }
            }
            if ($arrBoolMaCK) {
                $errorBool = true;
                $this->view->errorAmountCK = 'Số lượng chứng khoán không hợp lệ';
            }
        }
        //End so luong

        //Get balance
        $arrBalance = Service_Model::factory('Balance', 'getBalanceByUser', array('userid' => $arrData['userid']), 'frontend');

        //Check balance
        $errorBalance = false;
        if (!empty($arrBalance)) {
            if ($arrBalance['totalcredit'] < 0) {
                $errorBalance = true;
            }
            else {
                if(count($arrData['mack']) > $arrBalance['totalcredit']) {
                    $errorBalance = true;
                }
            }
        }
        else {
            $errorBalance = true;
        }

        if($errorBalance) {
            $errorBool = true;
            $this->view->errorBalance = 'Chưa có credit, xin liên hệ <a href="mailto:admin@chungkhoanngaymai.com">admin@chungkhoanngaymai.com</a> để nạp credit';
        }        
        //End balance

        if ($errorBool) {
            $this->view->errorFlag = true;
        }
    }
    //End func
    
}

?>
